package com.since.data_structure.binarytree.node;

/**
 * Created by Sincerity on 2020/5/21.
 * 描述： 二叉树的节点
 */
public class Node {
    public Node left;
    public Node right;
    public int value;
    //说明
    //1. 如果leftType == 0 表示指向的是左子树, 如果 1 则表示指向前驱结点
    //2. 如果rightType == 0 表示指向是右子树, 如果 1表示指向后继结点
    private int leftType;
    private int rightType;

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public Node(int value) {
        this.value = value;
    }


    public Node prologueSearch(int no) {
        if (this.value == no) {
            return this;
        }
        Node node = null;
        if (this.left != null) {
            node = this.left.prologueSearch(no);
        }
        if (node != null) {
            return node;
        }
        if (this.right != null) {
            node = this.right.prologueSearch(no);
        }
        return node;
    }

    public Node infixSearch(int no) {
        Node node = null;
        if (this.left != null) {
            node = this.left.prologueSearch(no);
        }
        if (node != null) {
            return node;
        }
        if (this.value == no) {
            return this;
        }
        if (this.right != null) {
            node = this.right.prologueSearch(no);
        }
        return node;
    }

    public Node postSearch(int no) {
        Node node = null;
        if (this.left != null) {
            node = this.left.prologueSearch(no);
        }
        if (node != null) {
            return node;
        }

        if (this.right != null) {
            node = this.right.prologueSearch(no);
        }
        if (this.value == no) {
            return this;
        }
        return node;
    }

    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.value > this.value) {
            if (this.right == null) this.right = node;
            else this.right.add(node);
        } else if (node.value < this.value) {
            this.left = node;
        } else {
            if (this.left == null) this.right = node;
            else this.left.add(left);
        }
    }

    //前序
    public void preOder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOder();
        }
        if (this.right != null) {
            this.right.preOder();
        }
    }

    //中序
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    //后续
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

    public void delete(int no) {
        if (this.left.value == no) {
            this.left = null;
        }
        if (this.right.value == no) {
            this.right = null;
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

}
